﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Core
{
    public static class ItemListerHelper
    {
        public static CMSPage CurrentFullViewPage
        {
            get { return ContextItem.GetItem("ItemListModule.CurrentFullViewPage", () => new CMSPage()); }
            set { ContextItem.SetItem("ItemListModule.CurrentFullViewPage", value); }
        }

        public static string GetItemUrl<T>(T item) where T : class, IItemListable
        {
            string itemUrl = "";
            var properties = item.GetType().GetProperties().Where(i => i.GetCustomAttributes(typeof(UrlAttribute), true).ToList().Count > 0).ToList();
            if (properties.Count > 0)
            {
                foreach (var p in properties)
                {
                    itemUrl += p.GetValue(item, null).ToString() + "/";
                }
                itemUrl = itemUrl.Substring(0, itemUrl.Length - 1);
            }
            return itemUrl;
        }
    }
}
